home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / PPP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-23  |  20.9 KB  |  777 lines

  1. /*
  2.  *  PPP.C    -- Send and receive datagrams on serial lines with
  3.  *           Point-to-Point Protocol
  4.  *
  5.  *    This implementation of PPP is declared to be in the public domain.
  6.  *
  7.  *    Based (in part) upon previous implementations by:
  8.  *    1989    -- Drew Daniel Perkins        (ddp@andrew.cmu.edu)
  9.  *           Carnegie Mellon University
  10.  *    09-90    -- Katie Stevens        (dkstevens@ucdavis.edu)
  11.  *           UC Davis, Computing Services
  12.  *
  13.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  14.  *        Computer Systems Consulting Services
  15.  *
  16.  *    Feb 91    Glenn McGregor            (ghm@merit.edu)
  17.  *        Testing and suggestions.
  18.  *
  19.  *    May 91    Bill Simpson & Glenn McGregor
  20.  *        Update to newest LCP and IPCP draft RFCs.
  21.  *        Add quick installation features.
  22.  *        Add support for echo and discard message sending.
  23.  *
  24.  *    Jul 91    Glenn McGregor & Bill Simpson
  25.  *        Improve PAP user interface and fix related bugs.
  26.  *        Remove kwaits and "phase machine".
  27.  */
  28.  
  29. #include "global.h"
  30. #include "mbuf.h"
  31. #include "proc.h"
  32. #include "iface.h"
  33. #include "slhc.h"
  34. #ifdef UNIX
  35. #include "unixasy.h"
  36. #else
  37. #include "n8250.h"
  38. #endif
  39. #include "asy.h"
  40. #include "pktdrvr.h"
  41. #include "devparam.h"
  42. #include "ppp.h"
  43. #include "pppfsm.h"
  44. #include "ppplcp.h"
  45. #include "ppppap.h"
  46. #include "pppipcp.h"
  47. #include "trace.h"
  48.  
  49. #if !defined(_lint)
  50. static char rcsid[] OPTIONAL = "$Id: ppp.c,v 1.12 1996/12/23 22:44:37 root Exp root $";
  51. #endif
  52.  
  53. #ifdef PPP
  54. /* Routines local to this file */
  55. static struct mbuf *htonppp (struct ppp_hdr * ppp, struct mbuf * data);
  56.  
  57. static void ppp_log (struct ppp_s * ppp_p, const char *comment);
  58. static void ppp_error (struct ppp_s * ppp_p, struct mbuf * bp, const char *comment);
  59. static void ppp_skipped (struct ppp_s * ppp_p, struct mbuf * bp, const char *comment);
  60.  
  61. static int ppp_raw (struct iface * ifp, struct mbuf * data);
  62.  
  63. static void ppp_recv (int dev, void *p1, void *p2);
  64.  
  65. static int ppp_iostatus (struct iface * ifp, int command, int32 value);
  66. static int ppp_discard (struct iface * ifp, struct mbuf * bp);
  67. static int ppp_echo (struct iface * ifp, struct mbuf * bp);
  68.  
  69.  
  70. /*
  71.  * FCS lookup table as generated by fcsgen.c
  72.  */
  73. int16 fcstab[256] =
  74. {
  75.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  76.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  77.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  78.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  79.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  80.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  81.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  82.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  83.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  84.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  85.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  86.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  87.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  88.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  89.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  90.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  91.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  92.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  93.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  94.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  95.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  96.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  97.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  98.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  99.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  100.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  101.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  102.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  103.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  104.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  105.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  106.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  107. };
  108.  
  109. #define pppfcs(fcs, c)        ((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
  110. #define SP_CHAR            0x20
  111.  
  112.  
  113.  
  114. /****************************************************************************/
  115.  
  116. /* Convert PPP header in host form to network form */
  117.  
  118. static struct mbuf *
  119. htonppp (struct ppp_hdr *ppp, struct mbuf *data)
  120. {
  121. struct mbuf *bp;
  122. register unsigned char *cp;
  123.  
  124.     /* Prepend header onto packet data */
  125.     if ((bp = pushdown (data, PPP_HDR_LEN)) == NULLBUF) {
  126.         free_p (data);
  127.         return NULLBUF;
  128.     }
  129.     /* Load header with proper values */
  130.     cp = bp->data;
  131.     *cp++ = ppp->addr;
  132.     *cp++ = ppp->control;
  133.     cp = put16 (cp, ppp->protocol);
  134.  
  135.     return bp;
  136. }
  137.  
  138.  
  139.  
  140. /************************************************************************/
  141. /* General log routine */
  142.  
  143. static void
  144. ppp_log (struct ppp_s *ppp_p, const char *comment)
  145. {
  146.     if (ppp_p->trace)
  147.         trace_log (ppp_p->iface, "%s PPP %s", ppp_p->iface->name, comment);
  148. }
  149.  
  150.  
  151.  
  152. /* Bad packet */
  153. static void
  154. ppp_error (struct ppp_s *ppp_p, struct mbuf *bp, const char *comment)
  155. {
  156.     free_p (bp);
  157.     ppp_log (ppp_p, comment);
  158. }
  159.  
  160.  
  161.  
  162. /* Unknown type input packet */
  163. static void
  164. ppp_skipped (struct ppp_s *ppp_p, struct mbuf *bp, const char *comment)
  165. {
  166. struct ipcp_s *ipcp_p;
  167.  
  168.     if ((ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL)
  169.         (void) slhc_toss (ipcp_p->slhcp);
  170.     ppp_error (ppp_p, bp, comment);
  171. }
  172.  
  173.  
  174.  
  175. /****************************************************************************/
  176. /* Send IP datagram with Point-to-Point Protocol */
  177. int
  178. ppp_send (
  179. struct mbuf *bp,        /* Buffer to send */
  180. struct iface *ifp,        /* Pointer to interface control block */
  181. uint32 gateway OPTIONAL,    /* Ignored (PPP is point-to-point) */
  182. int prec OPTIONAL,
  183. int del OPTIONAL,
  184. int tput OPTIONAL,
  185. int rel OPTIONAL
  186. ) {
  187. struct ppp_s *ppp_p;
  188. struct ipcp_s *ipcp_p;
  189. int protocol = PPP_IP_PROTOCOL;
  190.  
  191.     if (ifp == NULLIF || (ppp_p = ifp->edv) == NULL) {
  192.         free_p (bp);
  193.         return -1;
  194.     }
  195.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  196.         ppp_error (ppp_p, bp, "not open for IP traffic");
  197.         ppp_p->OutError++;
  198.         return -1;
  199.     }
  200.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  201.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  202.         /* Attempt IP/TCP header compression */
  203.         switch (slhc_compress (ipcp_p->slhcp, &bp, ipcp_p->remote.work.slot_compress)) {
  204.             case SL_TYPE_IP:
  205.                 protocol = PPP_IP_PROTOCOL;
  206.                 break;
  207.             case SL_TYPE_COMPRESSED_TCP:
  208.                 protocol = PPP_COMPR_PROTOCOL;
  209.                 break;
  210.             case SL_TYPE_UNCOMPRESSED_TCP:
  211.                 protocol = PPP_UNCOMP_PROTOCOL;
  212.                 break;
  213.             default:
  214.                 ppp_error (ppp_p, bp, "bad IP packet");
  215.                 ppp_p->OutError++;
  216.                 return -1;
  217.         }
  218.     }
  219.     return (*ifp->output) (ifp, NULLCHAR, NULLCHAR, (int16) protocol, bp);
  220. }
  221.  
  222.  
  223.  
  224. /* Send a packet with PPP header */
  225. int
  226. ppp_output (
  227. struct iface *ifp,        /* Pointer to interface control block */
  228. const char *dest OPTIONAL,    /* Dest addr (ignored; PPP is point-to-point) */
  229. char *source OPTIONAL,        /* Source addr (ignored; PPP is point-to-point) */
  230. int16 protocol,            /* PPP Protocol Type field */
  231. struct mbuf *data        /* Actual data to be sent */
  232. ) {
  233. struct ppp_s *ppp_p;
  234. struct mbuf *bp;
  235. struct ppp_hdr hdr;
  236.  
  237.     if (ifp == NULLIF || (ppp_p = ifp->edv) == NULL) {
  238.         free_p (data);
  239.         return -1;
  240.     }
  241.     if (ppp_p->phase == pppDEAD) {
  242.         ppp_error (ppp_p, data, "line not up");
  243.         ppp_p->OutError++;
  244.         return -1;
  245.     }
  246.     hdr.addr = HDLC_ALL_ADDR;
  247.     hdr.control = HDLC_UI;
  248.     hdr.protocol = protocol;
  249.  
  250.     if ((bp = htonppp (&hdr, data)) == NULLBUF) {
  251.         ppp_log (ppp_p, Nospace);
  252.         ppp_p->OutMemory++;
  253.         return -1;
  254.     }
  255.     return (*ifp->raw) (ifp, bp);
  256. }
  257.  
  258.  
  259.  
  260. /* Encode a raw packet in PPP framing, put on link output queue */
  261. static int
  262. ppp_raw (struct iface *ifp, struct mbuf *bp)
  263. {
  264. struct ppp_s *ppp_p = ifp->edv;
  265. struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  266. int full_lcp, full_ac, full_p;
  267. int16 calc_fcs = HDLC_FCS_START;
  268. uint32 accm = LCP_ACCM_DEFAULT;
  269. struct ppp_hdr ph;
  270. int len = PPP_HDR_LEN;
  271. struct mbuf *vbp, **bpp;
  272. register unsigned char *cp;
  273. register int c;
  274.  
  275.     dump (ifp, IF_TRACE_OUT, CL_PPP, bp);
  276.     ppp_p->OutTxOctetCount += len_p (bp) + 2;    /* count FCS bytes */
  277.     ifp->rawsndcnt++;
  278.     ifp->lastsent = secclock ();
  279.  
  280.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  281.     if (bp == NULLBUF || bp->cnt < PPP_HDR_LEN) {
  282.         ppp_error (ppp_p, bp, "link header missing");
  283.         ppp_p->OutError++;
  284.         return -1;
  285.     }
  286.     ph.addr = bp->data[0];
  287.     ph.control = bp->data[1];
  288.     ph.protocol = get16 ((char *) &bp->data[2]);
  289.  
  290.     if ((full_lcp = (ph.protocol == PPP_LCP_PROTOCOL)) == 0 && (lcp_p->remote.work.negotiate & LCP_N_ACCM))    /*lint !e731 */
  291.         accm = lcp_p->remote.work.accm;
  292.  
  293.     if ((full_p = (full_lcp || !(lcp_p->remote.work.negotiate & LCP_N_PFC) || ph.protocol >= 0x00ff)) == 0)    /*lint !e731 */
  294.         --len;
  295.  
  296.     /* Discard HDLC address and control fields if possible */
  297.     if ((full_ac = (full_lcp || !(lcp_p->remote.work.negotiate & LCP_N_ACFC))) == 0)    /*lint !e731 */
  298.         len -= 2;
  299.  
  300.     /* Load header with proper values */
  301.     bp->cnt -= (int16) (PPP_HDR_LEN - len);
  302.     cp = (bp->data += PPP_HDR_LEN - len);
  303.  
  304.     if (full_ac) {
  305.         *cp++ = ph.addr;
  306.         *cp++ = ph.control;
  307.     }
  308.     if (full_p)
  309.         *cp++ = (ph.protocol >> 8);
  310.     *cp++ = (ph.protocol & 0x00ff);
  311.  
  312.     /* Allocate output mbuf that's twice as long as the packet.
  313.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  314.      */
  315.     if ((vbp = alloc_mbuf ((int16) (2 * len_p (bp) + HDLC_ENVLEN))) == NULLBUF) {
  316.         ppp_error (ppp_p, bp, Nospace);
  317.         ppp_p->OutMemory++;
  318.         return -1;
  319.     }
  320.     cp = vbp->data;
  321.  
  322.     /* No need to send an opening flag if the previous packet is still
  323.      * being transmitted.
  324.      */
  325. #ifdef UNIX
  326.     if (Asy[ifp->dev].sndq == 0) {
  327. #else
  328.     if (Asy[ifp->dev].dma.flags == 0) {
  329. #endif
  330.         /* Flush out any line garbage */
  331.         *cp++ = HDLC_FLAG;
  332.         ppp_p->OutOpenFlag++;
  333.     }
  334.     /* Copy input to output, escaping special characters */
  335.     bpp = &bp;
  336.     while ((c = PULLCHAR (bpp)) != -1) {
  337.         /* Fold char value into FCS calculated so far */
  338.         calc_fcs = pppfcs (calc_fcs, c);
  339.  
  340.         if (((c < SP_CHAR) && (accm & (1L << c))) || (c == HDLC_ESC_ASYNC) || (c == HDLC_FLAG)) {
  341.             *cp++ = HDLC_ESC_ASYNC;
  342.             *cp++ = uchar(c ^ HDLC_ESC_COMPL);
  343.         } else
  344.             *cp++ = uchar(c);
  345.     }
  346.  
  347.     /* Final FCS calculation */
  348.     calc_fcs ^= 0xffff;
  349.     c = (calc_fcs & 0x00ff);/* Least significant byte first */
  350.     if (((c < SP_CHAR) && (accm & (1L << c))) || (c == HDLC_ESC_ASYNC) || (c == HDLC_FLAG)) {
  351.         *cp++ = HDLC_ESC_ASYNC;
  352.         *cp++ = uchar(c ^ HDLC_ESC_COMPL);
  353.     } else
  354.         *cp++ = uchar(c);
  355.  
  356.     c = (calc_fcs >> 8);    /* Most significant byte next */
  357.     if (((c < SP_CHAR) && (accm & (1L << c))) || (c == HDLC_ESC_ASYNC) || (c == HDLC_FLAG)) {
  358.         *cp++ = HDLC_ESC_ASYNC;
  359.         *cp++ = uchar(c ^ HDLC_ESC_COMPL);
  360.     } else
  361.         *cp++ = uchar(c);
  362.  
  363.     /* Tie off the packet */
  364.     *cp++ = HDLC_FLAG;
  365.     vbp->cnt = (int16) (cp - vbp->data);
  366.  
  367.     if (ifp->trace & IF_TRACE_RAW)
  368.         raw_dump (ifp, IF_TRACE_OUT, vbp);
  369.     return asy_send (ifp->dev, vbp);
  370. }
  371.  
  372.  
  373.  
  374. /****************************************************************************/
  375. /* Packetize PPP input from device */
  376. /* (process started by ppp_init) */
  377.  
  378. void
  379. ppp_recv (int dev, void *p1, void *p2 OPTIONAL)
  380. {
  381. struct iface *ifp = p1;
  382. struct ppp_s *ppp_p = ifp->edv;
  383. uint32 accm = LCP_ACCM_DEFAULT;
  384. int16 calc_fcs = HDLC_FCS_START;
  385. struct mbuf *raw_bp = NULLBUF;
  386. struct mbuf *head_bp = NULLBUF;
  387. register struct mbuf *tail_bp = NULLBUF;
  388. unsigned char *cp = (unsigned char *)0;        /* next byte in tail mbuf */
  389. register int mode = FALSE;
  390. register int c;
  391.  
  392.     while ((c = get_asy (dev)) != -1) {
  393. #ifdef PPP_DEBUG_RAW
  394.         if (ifp->trace & IF_TRACE_RAW) {
  395.             if (raw_bp != NULLBUF || (raw_bp = alloc_mbuf (LCP_MRU_HI * 2)) != NULLBUF) {
  396.                 *raw_bp->data++ = c;
  397.                 raw_bp->cnt++;
  398.                 if (raw_bp->cnt != 1 && c == HDLC_FLAG) {
  399.                     raw_bp->data = (char *) (raw_bp + 1);
  400.                     raw_dump (ifp, IF_TRACE_IN, raw_bp);
  401.                     raw_bp->cnt = 0;
  402.                 }
  403.             }
  404.         }
  405. #endif
  406.         if (c == HDLC_FLAG) {
  407.             if (mode & PPP_ESCAPED) {
  408.                 ppp_skipped (ppp_p, head_bp, "deliberate cancellation");
  409.                 ppp_p->InFrame++;
  410.             } else if (mode & PPP_TOSS)
  411.                 free_p (head_bp);
  412.             else if (head_bp != NULLBUF) {
  413.                 if (calc_fcs != HDLC_FCS_FINAL) {
  414.                     ppp_skipped (ppp_p, head_bp, "checksum error");
  415.                     ppp_p->InChecksum++;
  416.                 } else {
  417.                     /* trim off FCS bytes */
  418.                     trim_mbuf (&head_bp, len_p (head_bp) - 2);
  419.  
  420.                     (void) net_route (ifp, CL_PPP, head_bp);
  421.                 }
  422.             } else
  423.                 ppp_p->InOpenFlag++;
  424.  
  425.             /* setup for next buffer */
  426.             mode = FALSE;
  427.             head_bp = tail_bp = NULLBUF;
  428.             calc_fcs = HDLC_FCS_START;
  429.             accm = LCP_ACCM_DEFAULT;
  430.  
  431.             /* Use negotiated values if LCP finished */
  432.             if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  433.                 struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  434.  
  435.                 if (lcp_p->local.work.negotiate & LCP_N_ACCM)
  436.                     accm = lcp_p->local.work.accm;
  437.             }
  438. #ifdef PPP_DEBUG_RAW
  439.             if (!(ifp->trace & IF_TRACE_RAW)) {
  440.                 if (raw_bp != NULLBUF) {
  441.                     free_p (raw_bp);
  442.                     raw_bp = NULLBUF;
  443.                 }
  444.             }
  445. #endif
  446.             continue;
  447.         }
  448.         /* We reach here for every byte inside a frame.
  449.          * (The order of the following tests is important.)
  450.          * Discard spurious control characters.
  451.          * Check for escape sequence.
  452.          * (Allow escaped escape.)
  453.          */
  454.         if (c < SP_CHAR && (accm & (1L << c)))
  455.             continue;
  456.         else if (mode & PPP_ESCAPED) {
  457.             mode &= ~PPP_ESCAPED;
  458.             c ^= HDLC_ESC_COMPL;
  459.         } else if (c == HDLC_ESC_ASYNC) {
  460.             mode |= PPP_ESCAPED;
  461.             continue;
  462.         }
  463.         /* We reach here with a byte for the buffer.
  464.          * Make sure there is room for it.
  465.          */
  466.         if (tail_bp == NULLBUF) {
  467.             if ((tail_bp = alloc_mbuf (PPP_ALLOC)) == NULLBUF) {
  468.                 ppp_skipped (ppp_p, tail_bp, Nospace);
  469.                 ppp_p->InMemory++;
  470.                 mode |= PPP_TOSS;
  471.                 continue;
  472.             }
  473.             head_bp = tail_bp;
  474.             cp = tail_bp->data;
  475.         } else if (tail_bp->cnt >= tail_bp->size) {
  476.             /* Current mbuf is full */
  477.             if ((tail_bp->next = alloc_mbuf (PPP_ALLOC)) == NULLBUF) {
  478.                 /* No memory, drop the whole packet */
  479.                 ppp_skipped (ppp_p, head_bp, Nospace);
  480.                 ppp_p->InMemory++;
  481.                 head_bp = NULLBUF;
  482.                 mode |= PPP_TOSS;
  483.                 continue;
  484.             }
  485.             tail_bp = tail_bp->next;
  486.             cp = tail_bp->data;
  487.         }
  488.         /* Store the byte, increment counts */
  489.         if (cp)
  490.             *cp++ = uchar(c);
  491.         tail_bp->cnt++;
  492.         calc_fcs = pppfcs (calc_fcs, c);
  493.     }
  494.  
  495.     /* clean up afterward */
  496.     free_p (raw_bp);
  497.     free_p (head_bp);
  498.     ifp->rxproc = NULLPROC;
  499. }
  500.  
  501.  
  502.  
  503. /* Process incoming PPP packets */
  504. /* (called from network task) */
  505. void
  506. ppp_proc (struct iface *ifp, struct mbuf *bp)
  507. {
  508. struct ppp_s *ppp_p;
  509. struct ipcp_s *ipcp_p;
  510. struct ppp_hdr ph;
  511. struct mbuf *hbp, **bpp;
  512. int16 negotiated = FALSE;
  513.  
  514.     if (ifp == NULLIF) {
  515.         log (-1, "ppp_proc: missing iface");
  516.         return;
  517.     }
  518.     if (bp == NULLBUF) {
  519.         trace_log (ifp, "ppp_proc: missing buffer");
  520.         return;
  521.     }
  522.     ppp_p = ifp->edv;
  523.     ppp_p->InRxOctetCount += len_p (bp) + 2;    /* count FCS bytes */
  524.  
  525.     /* Use negotiated values if LCP finished */
  526.     if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  527.         struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  528.  
  529.         negotiated = lcp_p->local.work.negotiate;
  530.     }
  531.     /* HDLC address and control fields may be compressed out */
  532.     if ((byte_t) bp->data[0] != HDLC_ALL_ADDR) {
  533.         if (!(negotiated & LCP_N_ACFC)) {
  534.             ppp_skipped (ppp_p, bp, "missing ALL address");
  535.             ppp_p->InFrame++;
  536.             return;
  537.         }
  538.     } else if ((byte_t) bp->data[1] != HDLC_UI) {
  539.         if (!(negotiated & LCP_N_ACFC) || !(negotiated & LCP_N_PFC)) {
  540.             ppp_skipped (ppp_p, bp, "missing UI");
  541.             ppp_p->InFrame++;
  542.             return;
  543.         }
  544.     } else     /* skip address/control fields */
  545.         (void) pull16 (&bp);
  546.  
  547.     /* Initialize the expected header */
  548.     ph.addr = HDLC_ALL_ADDR;
  549.     ph.control = HDLC_UI;
  550.     bpp = &bp;
  551.     ph.protocol = (int16) PULLCHAR (bpp);
  552.  
  553.     /* First byte of PPP protocol field may be compressed out */
  554.     if (ph.protocol & 0x01) {
  555.         if (!(negotiated & LCP_N_PFC)) {
  556.             ppp_skipped (ppp_p, bp, "missing upper protocol byte");
  557.             ppp_p->InFrame++;
  558.             return;
  559.         }
  560.     } else {
  561.         ph.protocol = (int16) ((ph.protocol << 8) | PULLCHAR (bpp));
  562.  
  563.         /* Second byte of PPP protocol field must be odd */
  564.         if (!(ph.protocol & 0x01)) {
  565.             ppp_skipped (ppp_p, bp, "missing lower protocol byte");
  566.             ppp_p->InFrame++;
  567.             return;
  568.         }
  569.     }
  570.  
  571.  
  572.     switch (ph.protocol) {
  573.         case PPP_IP_PROTOCOL:    /* Regular IP */
  574.             if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  575.                 ppp_error (ppp_p, bp, "not open for IP traffic");
  576.                 ppp_p->InError++;
  577.                 break;
  578.             }
  579.             (void) ip_route (ifp, bp, 0);
  580.             break;
  581.  
  582.         case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  583.             if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  584.                 ppp_skipped (ppp_p, bp, "not open for Compressed TCP/IP traffic");
  585.                 ppp_p->InError++;
  586.                 break;
  587.             }
  588.             ipcp_p = ppp_p->fsm[IPcp].pdv;
  589.             if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  590.                 ppp_skipped (ppp_p, bp, "Compressed TCP/IP not enabled");
  591.                 ppp_p->InError++;
  592.                 break;
  593.             }
  594.             if (slhc_uncompress (ipcp_p->slhcp, &bp) <= 0) {
  595.                 ppp_error (ppp_p, bp, "Compressed TCP/IP packet error");
  596.                 ppp_p->InError++;
  597.                 break;
  598.             }
  599.             (void) ip_route (ifp, bp, 0);
  600.             break;
  601.  
  602.         case PPP_UNCOMP_PROTOCOL:    /* Van Jacobson Uncompressed TCP/IP */
  603.             if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  604.                 ppp_skipped (ppp_p, bp, "not open for Uncompressed TCP/IP traffic");
  605.                 ppp_p->InError++;
  606.                 break;
  607.             }
  608.             ipcp_p = ppp_p->fsm[IPcp].pdv;
  609.             if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  610.                 ppp_skipped (ppp_p, bp, "Uncompressed TCP/IP not enabled");
  611.                 ppp_p->InError++;
  612.                 break;
  613.             }
  614.             if (slhc_remember (ipcp_p->slhcp, &bp) <= 0) {
  615.                 ppp_error (ppp_p, bp, "Uncompressed TCP/IP packet error");
  616.                 ppp_p->InError++;
  617.                 break;
  618.             }
  619.             (void) ip_route (ifp, bp, 0);
  620.             break;
  621.  
  622.         case PPP_LCP_PROTOCOL:    /* Link Control Protocol */
  623.             ppp_p->InNCP[Lcp]++;
  624.             fsm_proc (&(ppp_p->fsm[Lcp]), bp);
  625.             break;
  626.  
  627.         case PPP_PAP_PROTOCOL:    /* Password Authenticate Protocol */
  628.             if (ppp_p->phase != pppAP && ppp_p->phase != pppREADY) {
  629.                 ppp_error (ppp_p, bp, "not ready for Authentication");
  630.                 ppp_p->InError++;
  631.                 break;
  632.             }
  633.             ppp_p->InNCP[Pap]++;
  634.             pap_proc (&(ppp_p->fsm[Pap]), bp);
  635.             break;
  636.  
  637.         case PPP_IPCP_PROTOCOL:    /* IP Control Protocol */
  638.             if (ppp_p->phase != pppREADY) {
  639.                 ppp_error (ppp_p, bp, "not ready for IPCP traffic");
  640.                 ppp_p->InError++;
  641.                 break;
  642.             }
  643.             ppp_p->InNCP[IPcp]++;
  644.             fsm_proc (&(ppp_p->fsm[IPcp]), bp);
  645.             break;
  646.  
  647.         default:
  648.             if (ppp_p->trace)
  649.                 trace_log (ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  650.                     ppp_p->iface->name, ph.protocol);
  651.             ppp_p->InUnknown++;
  652.  
  653.             /* Build Protocol Reject packet:
  654.              * put the header back on ...
  655.              */
  656.             if ((hbp = htonppp (&ph, bp)) == NULLBUF) {
  657.                 ppp_log (ppp_p, Nospace);
  658.                 ppp_p->InMemory++;
  659.                 return;
  660.             }
  661.             /* ... then pull off the address and control fields ... */
  662.             (void) pull16 (&hbp);
  663.             /* ... and send it as an LCP packet */
  664.             (void) fsm_send (&(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp);
  665.             break;
  666.     }
  667. }
  668.  
  669.  
  670.  
  671. /************************************************************************/
  672.  
  673. /* Keep track of changes in I-O status */
  674. /* (called through iface iostatus vector) */
  675.  
  676. static int
  677. ppp_iostatus (struct iface *ifp, int command, int32 value OPTIONAL)
  678. {
  679. struct ppp_s *ppp_p = ifp->edv;
  680.  
  681.     switch (command) {
  682.         case PARAM_UP:
  683.             ppp_log (ppp_p, "Physical layer up");
  684.  
  685.             if (ppp_p->phase == pppDEAD)
  686.                 ppp_p->phase = pppLCP;
  687.  
  688.             fsm_start (&(ppp_p->fsm[Lcp]));
  689.             return 0;
  690.  
  691.         case PARAM_DOWN:
  692.             ppp_log (ppp_p, "Physical layer down");
  693.  
  694.             fsm_down (&(ppp_p->fsm[Lcp]));
  695.             ppp_p->phase = pppDEAD;
  696.             return 0;
  697.         default:
  698.             break;
  699.     }
  700.     return -1;
  701. }
  702.  
  703.  
  704.  
  705. static int
  706. ppp_discard (struct iface *ifp, struct mbuf *bp)
  707. {
  708. struct ppp_s *ppp_p = ifp->edv;
  709.  
  710.     return fsm_send (&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  711. }
  712.  
  713.  
  714.  
  715. static int
  716. ppp_echo (struct iface *ifp, struct mbuf *bp)
  717. {
  718. struct ppp_s *ppp_p = ifp->edv;
  719.  
  720.     return fsm_send (&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  721. }
  722.  
  723.  
  724.  
  725. /****************************************************************************/
  726. /* Initialize PPP control structures for a Point-to-Point interface */
  727. int
  728. ppp_init (struct iface *ifp)
  729. {
  730. struct ppp_s *ppp_p;
  731. char *ifn;
  732.  
  733. #ifdef MSDOS
  734.         /* Setup for Point-to-Point Protocol */
  735.         ifp->ioctl = asy_ioctl;
  736. #endif
  737.     ppp_p = callocw (1, sizeof (struct ppp_s));
  738.  
  739.     ifp->edv = ppp_p;
  740.     ifp->iostatus = ppp_iostatus;
  741.     ifp->raw = ppp_raw;
  742.     ifp->show = ppp_show;
  743.     ifp->echo = ppp_echo;
  744.     ifp->discard = ppp_discard;
  745.  
  746.     ppp_p->iface = ifp;
  747.     ppp_p->phase = pppDEAD;
  748.  
  749.     lcp_init (ppp_p);
  750.     pap_init (ppp_p);
  751.     ipcp_init (ppp_p);
  752.  
  753.     ifp->rxproc = newproc (ifn = if_name (ifp, " receive"), 320, ppp_recv, ifp->dev, ifp, NULL, 0);
  754.     free (ifn);
  755.     return 0;
  756. }
  757.  
  758.  
  759.  
  760. int
  761. ppp_free (struct iface *ifp)
  762. {
  763. struct ppp_s *ppp_p = ifp->edv;
  764. int fsmi;
  765.  
  766.     alert (ifp->rxproc, -1);
  767.  
  768.     for (fsmi = Lcp; fsmi < fsmi_Size; )
  769.         fsm_free (&(ppp_p->fsm[fsmi++]));
  770.  
  771.     free (ppp_p->peername);
  772.     free (ppp_p);
  773.     return 0;
  774. }
  775.  
  776. #endif /* PPP */
  777.